let timeout = null;
let loading;
import Vue from 'vue'
const bus = new Vue()
import { Loading, MessageBox } from "element-ui";
export const dateFormat = date => {
  const time = new Date(date);
  const y = time.getFullYear();
  const m =
    time.getMonth() + 1 < 10 ? `0${time.getMonth() + 1}` : time.getMonth() + 1;
  const d = time.getDate() < 10 ? `0${time.getDate()}` : time.getDate();
  let h = time.getHours() >= 10 ? time.getHours() : `0${time.getHours()}`;
  let min =
    time.getMinutes() >= 10 ? time.getMinutes() : `0${time.getMinutes()}`;
  let s = time.getSeconds() >= 10 ? time.getSeconds() : `0${time.getSeconds()}`;
  return `${y}-${m}-${d} ${h}:${min}:${s}`;
};

export const debounce = (fn, wait) => {
  if (timeout !== null) clearTimeout(timeout);
  timeout = setTimeout(fn, wait);
};
export const dateFormatArray = dateArray => {
  if (!dateArray[0]) {
    return;
  }
  const startTime = new Date(dateArray[0]);
  const endTime = new Date(dateArray[1]);
  const sy = startTime.getFullYear();
  const sm =
    startTime.getMonth() + 1 < 10
      ? `0${startTime.getMonth() + 1}`
      : startTime.getMonth() + 1;
  const sd =
    startTime.getDate() < 10 ? `0${startTime.getDate()}` : startTime.getDate();
  const ey = endTime.getFullYear();
  const em =
    endTime.getMonth() + 1 < 10
      ? `0${endTime.getMonth() + 1}`
      : endTime.getMonth() + 1;
  const ed =
    endTime.getDate() < 10 ? `0${endTime.getDate()}` : endTime.getDate();
  return `${sy}-${sm}-${sd}-${ey}-${em}-${ed}`;
};
// 连接符
export const dateFormatT = dateArray => {
  if (!dateArray[0]) {
    return;
  }
  const startTime = new Date(dateArray[0]);
  const endTime = new Date(dateArray[1]);
  const sy = startTime.getFullYear();
  const sm =
    startTime.getMonth() + 1 < 10
      ? `0${startTime.getMonth() + 1}`
      : startTime.getMonth() + 1;
  const sd =
    startTime.getDate() < 10 ? `0${startTime.getDate()}` : startTime.getDate();
  const ey = endTime.getFullYear();
  const em =
    endTime.getMonth() + 1 < 10
      ? `0${endTime.getMonth() + 1}`
      : endTime.getMonth() + 1;
  const ed =
    endTime.getDate() < 10 ? `0${endTime.getDate()}` : endTime.getDate();
  return `${sy}-${sm}-${sd}~${ey}-${em}-${ed}`;
};

export const download = (data, name) => {
  const dom = document.createElement("a"); // 隐藏的a标签，href为输出流
  const blob = new Blob([data], { type: "application/vnd.ms-excel" }); // 创建一个类文件对象：Blob对象表示一个不可变的、原始数据的类文件对象
  const url = window.URL.createObjectURL(blob); // 生成一个Blob对象
  dom.style.display = "none";
  dom.href = url;
  document.body.appendChild(dom);
  dom.setAttribute("download", name); // 指示浏览器下载url,而不是导航到它
  dom.click();
  document.body.removeChild(dom);
  // 手动释放创建的URL对象所占内存
  URL.revokeObjectURL(url);
};
export const startLoading = (text = "正在加载中") => {
  //   console.log("加载开始");
  loading = Loading.service({
    lock: true,
    text,
    background: "rgba(255,255,255,0.5)"
  });
};

export const endLoading = () => {
  loading && loading.close();
};
/**
 * @param {String} type   => success/info/warning/error
 */
export const ConfirmMsg = (type, title, content, success, cancel) => {
  MessageBox.confirm(content, title, {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type
  })
    .then(() => typeof success === 'function' && success())
    .catch(() => typeof cancel === 'function' && cancel()

    )
}
/**
 * @param {String} type       => /success / info / warning / error
 * @param {Object} title      => 提示: 
 * @param {String} msg        =>  是否删除...
 * @param {Promise} api       =>  getList...
 * @param {Object} params     =>  api的参数
 * @param {Function} success  =>  成功回调
 * @param {Function} cancel   =>  失败回调
 *  confirmFun(
        "warning",
        "提示：",
        "确定删除该数据？",
        delUserRenderConfig,
        id,
        (res) => {
          message("success", "图表删除成功");
          this.getUserRenderConfig();
        },
        (err) => {
          console.log(err);
        }
      );
 */
export const confirmFun = (type = "warning", title = "提示：", msg = "确定删除该数据？", api, params, success, cancel) => {
  ConfirmMsg(type, title, msg, () => {
    startLoading()
    api(params).then(res => {
      success(res)
      endLoading()
    }).catch(err => {
      cancel(err)
      endLoading()
    })
  })
}
/**
 * @param {String} type       => /success / info / warning / error
 */
export const Message = (type, message, duration = 3, maxCount = 5) => {

  bus.$message[type](
    message,
    duration,
    maxCount,
  )
}
export const getTokenAndID = () => {
  return `token=${sessionStorage.getItem("token")}&id=${sessionStorage.getItem(
    "subId"
  )}`;
};

export function selectColor(index) {
  let color = [
    // "#5892a9",
    // "#6d8a95",
    // "#999396",
    // "#a2a8b3",
    // "#71a6b5",
    // "#9eb2bf",
    // "#b2c4d4",
    // "#6190a2",
    // "#52a5bc",
    // "#81c5d3",
    "linear-gradient(-225deg, #77A1D3 0%, #79CBCA 0%, #E684AE 100%);",
    "linear-gradient(-225deg, #fad0c4 0%, #ff9a9e 100%)",
    "linear-gradient(-225deg, #7DE2FC 0%, #B9B6E5 100%);",
    "linear-gradient(-225deg, #6E7FF3 0%, #5753C9 100%);",
    "linear-gradient(-225deg, #64b3f4 0%, #c2e59c 100%);",
    "linear-gradient(-225deg, #00c6fb 0%, #005bea 100%);",
    "rgba(91, 143, 249, 1)",
    "rgba(246, 189, 22, 1)",
    "rgba(255, 152, 69, 1)",
    "rgba(93, 112, 146, 1)",
    "rgba(148, 95, 185, 1)",
    "rgba(109, 200, 236, 1)",
    "#218c74",
    "#E6A23C",
    "#6c5ce7",
    "#aaa69d",
    "#7BA79A",
    "#25597E",
    "#00BCD4",
    "#227093",
    "#DB7093",
    "#0183B7",
    "#CCCCFF",
    "#99CCFF",
    "#5BBCC5",
    "#70458C",
    "#01A371",
    "#2196F3",
    "linear-gradient(-225deg, #ff9a9e 0%, #ff0844 100%);",
    "rgb(99,123,123)",
    "linear-gradient(-225deg, #c471ed 0%, #f64f59 100%);",
    "rgb(49,49,123)",
    "#ef5350",
    "rgb(165,115,140)",
    "#F56C6C",
    "linear-gradient(-225deg, #1c92d2 0%, #06beb6 100%);",
  ];
  return color[index];
}
// 定义母系统域名前缀
export const getLastDomain = () => {
  return process.env.VUE_APP_ERP_DOMAIN;
  // return 'http://localhost:8080'
}